How To Provision A LEMP Stack In Vagrant

snippets of code taken from:
https://github.com/danielwrobert/vagrant-LEMP/blob/master/provision/setup.sh

https://github.com/panique/vagrant-lamp-bootstrap

This will install Ubuntu 16.04, NGINX, MariaDB, PHP7-FPM and Git

1. Initialize Vagrant

$ vagrant init bento/ubuntu-16.04

2. Edit Vagrantfile

Next, edit Vagrantfile and make it look something like this below (without the commented lines). This will create a symbolic link from the root folder with Vagrantfile in it >> to the virtual machine's ‘/var/www' folder with group and owner ‘www-data'. In the provisioning script, we will add an html folder which will be the root of our projects:

Vagrant.configure("2") do |config|
  
  config.vm.box = "bento/ubuntu-16.04"

  config.vm.network "forwarded_port", guest: 80, host: 80

  config.vm.synced_folder "./", "/var/www", create: true, group: "www-data", owner: "www-data"

  config.vm.provision "shell" do |s|
    s.path = "bootstrap.sh"
  end
  
end

3. Create a config directory

Inside your local root folder (where Vagrantfile is located) create a folder called ‘config'. Inside this root folder, you will now have a 1) ‘config' directory, 2) ‘.vagrant' directory, 3) ‘Vagrantfile' file.

4. Create the shell script

Inside the same local root folder (where Vagrantfile is located), create a file called ‘bootstrap.sh' and copy/paste the code below into it. Change the ‘PASSWORD' (and optionally PROJECTFOLDER) to whatever you like:

#!/bin/bash

# Using single quotes instead of double quotes to make it work with special-character passwords
PASSWORD='vagrant'
PROJECTFOLDER='html'

# create project folder
sudo mkdir "/var/www/${PROJECTFOLDER}"

# update / upgrade
echo "Updating apt-get..."
sudo apt-get update > /dev/null 2>&1
sudo apt-get -y upgrade > /dev/null 2>&1

# install git
echo "Installing Git..."
sudo apt-get install -y git > /dev/null 2>&1

# install nginx
echo "Installing Nginx..."
sudo apt-get install -y nginx > /dev/null 2>&1

# install php7-fpm
echo "Installing PHP..."
sudo apt-get install -y php-fpm php-mysql php-xml php-gd php7.0-zip > /dev/null 2>&1

# install mariadb and give password to installer
echo "Preparing MariaDB..."
sudo apt-get install -y debconf-utils > /dev/null 2>&1
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password password $PASSWORD"
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password_again password $PASSWORD"
sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
sudo add-apt-repository 'deb [arch=amd64,i386] http://sfo1.mirrors.digitalocean.com/mariadb/repo/10.3/ubuntu xenial main'

echo "Updating apt-get..."
sudo apt-get update > /dev/null 2>&1
 
echo "Installing MariaDB..."
sudo apt-get install -y mariadb-server

# Nginx Config
echo "Configuring Nginx..."
sudo cp /var/www/config/nginx_vhost /etc/nginx/sites-available/nginx_vhost > /dev/null 2>&1
sudo ln -s /etc/nginx/sites-available/nginx_vhost /etc/nginx/sites-enabled/

sudo rm -rf /etc/nginx/sites-enabled/default

# Restarting Nginx for config to take effect
echo "Restarting Nginx..."
sudo service nginx restart > /dev/null 2>&1

5. Configure NGINX server block

Inside the ‘config' directory, create a file and add the code below (in this case we name the file ‘nginx_vhost' which is also referenced in the code above):

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name localhost;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

6. Vagrant up

Finally, just vagrant up and let the script set up your virtual machine:

$ vagrant up

Alternative script that installs NGINX as reverse proxy for Apache, MariaDB, PHP7-FPM, Git and phpMyAdmin

ERRORS: Can't get phpmyadmin to install correctly and while apache and nginx install correctly, can't get them to work right.

#!/bin/bash

# Using single quotes instead of double quotes to make it work with special-character passwords
PASSWORD='12345678'
PROJECTFOLDER='html'

# create project folder
sudo mkdir "/var/www/${PROJECTFOLDER}"

# update / upgrade
echo "Updating apt-get..."
sudo apt-get update > /dev/null 2>&1
sudo apt-get -y upgrade > /dev/null 2>&1

# install git
echo "Installing Git..."
sudo apt-get install -y git > /dev/null 2>&1

# install nginx
echo "Installing Nginx..."
sudo apt-get install -y nginx > /dev/null 2>&1

# install php7-fpm
echo "Installing PHP..."
sudo apt-get install -y php-fpm php-mysql php-xml php-gd php7.0-zip > /dev/null 2>&1

# install mariadb and give password to installer
echo "Preparing MariaDB..."
sudo apt-get install -y debconf-utils > /dev/null 2>&1
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password password $PASSWORD"
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password_again password $PASSWORD"
sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
sudo add-apt-repository 'deb [arch=amd64,i386] http://sfo1.mirrors.digitalocean.com/mariadb/repo/10.3/ubuntu xenial main'

echo "Updating apt-get..."
sudo apt-get update > /dev/null 2>&1
 
echo "Installing MariaDB..."
sudo apt-get install -y mariadb-server

# Nginx Config
echo "Configuring Nginx..."
sudo cp /var/www/config/nginx_vhost /etc/nginx/sites-available/nginx_vhost > /dev/null 2>&1
sudo ln -s /etc/nginx/sites-available/nginx_vhost /etc/nginx/sites-enabled/

sudo rm -rf /etc/nginx/sites-enabled/default

# Restarting Nginx for config to take effect
echo "Restarting Nginx..."
sudo service nginx restart > /dev/null 2>&1

#install apache server for phpmyadmin
echo "Installing Apache..."
sudo apt-get install -y apache2 > /dev/null 2>&1

# setup hosts file
echo "Setting up host file..."
VHOST=$(cat <<EOF
<VirtualHost *:8000>
    DocumentRoot "/var/www/html"
    <Directory "/var/www/html">
        AllowOverride All
		Require all granted
    </Directory>
</VirtualHost>
EOF
)
echo "${VHOST}" > /etc/apache2/sites-available/000-default.conf

# add the php index file to the first position
VMOD=$(cat <<EOF
<IfModule mod_dir.c>
	DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
EOF
)
echo "${VMOD}" > /etc/apache2/mods-enabled/dir.conf

# modify virtualhost port
echo "modifying apache port..."
VPORT=$(cat <<EOF
Listen 8000

<IfModule ssl_module>
	Listen 443
</IfModule>

<IfModule mod_gnutls.c>
	Listen 443
</IfModule>
EOF
)
echo "${VPORT}" > /etc/apache2/ports.conf

# install apache module which takes care of logging the correct ip
echo "Installing libapache2-mod-rpaf for IP logging..."
sudo apt-get install -y libapache2-mod-rpaf

# enable mod_rewrite
sudo a2enmod rewrite > /dev/null 2>&1

# restart apache
sudo service apache2 restart > /dev/null 2>&1

# install phpmyadmin and give password(s) to installer
# for simplicity, the same password is used for mariadb and phpmyadmin
echo "Preparing phpmyadmin..."
sudo debconf-set-selections <<< "phpmyadmin phpmyadmin/dbconfig-install boolean true"
sudo debconf-set-selections <<< "phpmyadmin phpmyadmin/app-password-confirm password $PASSWORD"
sudo debconf-set-selections <<< "phpmyadmin phpmyadmin/mariadb/admin-pass password $PASSWORD"
sudo debconf-set-selections <<< "phpmyadmin phpmyadmin/mariadb/app-pass password $PASSWORD"
sudo debconf-set-selections <<< "phpmyadmin phpmyadmin/reconfigure-webserver multiselect none"

echo "Installing phpmyadmin..."
sudo apt-get install -y phpmyadmin

echo "Installing php7.0-mcrypt..."
sudo apt-get install mcrypt php7.0-mcrypt > /dev/null 2>&1

echo "Restarting php7.0-fpm..."
sudo service php7.0-fpm restart > /dev/null 2>&1

And this is the nginx server block with the phpmyadmin configuration added:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name localhost;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
	
# Phpmyadmin Configurations
    location /phpmyadmin {
       root /usr/share/;
       index index.php index.html index.htm;
       location ~ ^/phpmyadmin/(.+\.php)$ {
               try_files $uri =404;
               root /usr/share/;
               #fastcgi_pass 127.0.0.1:9000;
               #fastcgi_param HTTPS on; # <-- add this line
               fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
               fastcgi_index index.php;
               fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
               include fastcgi_params;
       }
       location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
               root /usr/share/;
       }
   }

   # Dealing with the uppercased letters
   location /phpMyAdmin {
       rewrite ^/* /phpmyadmin last;
   }

    location ~ /\.ht {
        deny all;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.